home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP09.ZIP / CHAP09 / PATRON / DOCUMENT.CPP < prev    next >
C/C++ Source or Header  |  1993-06-23  |  30KB  |  1,301 lines

  1. /*
  2.  * DOCUMENT.CPP
  3.  * Modifications for Chapter 9
  4.  *
  5.  * Implementation of the CPatronDoc derivation of CDocument that
  6.  * manages pages for us.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include <memory.h>
  19. #include "patron.h"
  20.  
  21.  
  22.  
  23. /*
  24.  * CPatronDoc::CPatronDoc
  25.  * CPatronDoc::~CPatronDoc
  26.  *
  27.  * Constructor Parameters:
  28.  *  hInst           HINSTANCE of the application.
  29.  */
  30.  
  31. CPatronDoc::CPatronDoc(HINSTANCE hInst)
  32.     : CDocument(hInst)
  33.     {
  34.     m_pPG=NULL;
  35.     m_lVer=VERSIONCURRENT;
  36.     m_pIStorage=NULL;
  37.     m_fPrintSetup=TRUE;
  38.     m_pDropTarget=NULL;
  39.  
  40.     //CHAPTER9MOD
  41.     m_cfEmbeddedObject  =RegisterClipboardFormat(CF_EMBEDDEDOBJECT);
  42.     m_cfObjectDescriptor=RegisterClipboardFormat(CF_OBJECTDESCRIPTOR);
  43.     //End CHAPTER9MOD
  44.  
  45.     return;
  46.     }
  47.  
  48.  
  49. CPatronDoc::~CPatronDoc(void)
  50.     {
  51.     if (NULL!=m_pDropTarget)
  52.         {
  53.         RevokeDragDrop(m_hWnd);
  54.         CoLockObjectExternal((LPUNKNOWN)m_pDropTarget, FALSE, TRUE);
  55.         m_pDropTarget->Release();
  56.         }
  57.  
  58.     if (NULL!=m_pPG)
  59.         delete m_pPG;
  60.  
  61.     if (NULL!=m_pIStorage)
  62.         m_pIStorage->Release();
  63.  
  64.     CoFreeUnusedLibraries();
  65.     return;
  66.     }
  67.  
  68.  
  69.  
  70.  
  71.  
  72. /*
  73.  * CPatronDoc::FInit
  74.  *
  75.  * Purpose:
  76.  *  Initializes an already created document window.  The client actually
  77.  *  creates the window for us, then passes that here for further
  78.  *  initialization.
  79.  *
  80.  * Parameters:
  81.  *  pDI             LPDOCUMENTINIT containing initialization parameters.
  82.  *
  83.  * Return Value:
  84.  *  BOOL            TRUE if the function succeeded, FALSE otherwise.
  85.  */
  86.  
  87. BOOL CPatronDoc::FInit(LPDOCUMENTINIT pDI)
  88.     {
  89.     //Change the stringtable range to our customization.
  90.     pDI->idsMin=IDS_DOCUMENTMIN;
  91.     pDI->idsMax=IDS_DOCUMENTMAX;
  92.  
  93.     //Do default initialization
  94.     if (!CDocument::FInit(pDI))
  95.         return FALSE;
  96.  
  97.     //Pages are created when we get a ::ULoad later.
  98.     return TRUE;
  99.     }
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106. /*
  107.  * CPatronDoc::FMessageHook
  108.  *
  109.  * Purpose:
  110.  *  Processes WM_SIZE for the document so we can resize the Pages window.
  111.  *
  112.  * Parameters:
  113.  *  <WndProc Parameters>
  114.  *  pLRes           LRESULT FAR * in which to store the return value
  115.  *                  for the message.
  116.  *
  117.  * Return Value:
  118.  *  BOOL            TRUE to prevent further processing, FALSE otherwise.
  119.  */
  120.  
  121. BOOL CPatronDoc::FMessageHook(HWND hWnd, UINT iMsg, WPARAM wParam
  122.     , LPARAM lParam, LRESULT FAR *pLRes)
  123.     {
  124.     UINT        dx, dy;
  125.     RECT        rc;
  126.  
  127.     if (WM_SIZE==iMsg && NULL!=m_pPG)
  128.         {
  129.         dx=LOWORD(lParam);
  130.         dy=HIWORD(lParam);
  131.  
  132.         //Resize the Pages window to fit the new client area of the document
  133.         GetClientRect(hWnd, &rc);
  134.         m_pPG->RectSet(&rc, FALSE);
  135.         }
  136.  
  137.     /*
  138.      * We return FALSE even on WM_SIZE so we can let the default procedure
  139.      * handle maximized MDI child windows appropriately.
  140.      */
  141.     return FALSE;
  142.     }
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151. /*
  152.  * CPatronDoc::Clear
  153.  *
  154.  * Purpose:
  155.  *  Sets all contents in the document back to defaults with no filename.
  156.  *
  157.  * Paramters:
  158.  *  None
  159.  *
  160.  * Return Value:
  161.  *  None
  162.  */
  163.  
  164. void CPatronDoc::Clear(void)
  165.     {
  166.     //Completely reset the pages
  167.     m_pPG->FIStorageSet(NULL, FALSE, FALSE);
  168.  
  169.     CDocument::Clear();
  170.     m_lVer=VERSIONCURRENT;
  171.     return;
  172.     }
  173.  
  174.  
  175.  
  176.  
  177. /*
  178.  * CPatronDoc::FDirtyGet
  179.  *
  180.  * Purpose:
  181.  *  Returns the current dirty status of the document.
  182.  *
  183.  * Parameters:
  184.  *  None
  185.  *
  186.  * Return Value:
  187.  *  BOOL            TRUE if the file is clean, FALSE otherwise.
  188.  */
  189.  
  190. BOOL CPatronDoc::FDirtyGet()
  191.     {
  192.     BOOL    fPageDirty;
  193.  
  194.     fPageDirty=m_pPG->FIsDirty();
  195.     return m_fDirty | fPageDirty;
  196.     }
  197.  
  198.  
  199.  
  200.  
  201.  
  202. /*
  203.  * CPatronDoc::Delete
  204.  *
  205.  * Purpose:
  206.  *  Removed the current object from the document.
  207.  *
  208.  * Paramters:
  209.  *  None
  210.  *
  211.  * Return Value:
  212.  *  None
  213.  */
  214.  
  215. void CPatronDoc::Delete(void)
  216.     {
  217.     if (NULL!=m_pPG)
  218.         m_pPG->TenantDestroy();
  219.  
  220.     CoFreeUnusedLibraries();
  221.     return;
  222.     }
  223.  
  224.  
  225.  
  226. /*
  227.  * CPatronDoc::FQueryPrinterSetup
  228.  *
  229.  * Purpose:
  230.  *  Returns whether or not the Printer Setup menu item can be
  231.  *  enabled.  Once you create a tenant in any page, Printer Setup
  232.  *  is voided simply to keep this sample simple, that is, we don't
  233.  *  have to worry about reorganizing potentially large amounts
  234.  *  of layout after we start plopping down objects.
  235.  *
  236.  * Parameters:
  237.  *  None
  238.  *
  239.  * Return Value:
  240.  *  BOOL            TRUE to enable the menu, FALSE otherwise.
  241.  */
  242.  
  243. BOOL CPatronDoc::FQueryPrinterSetup(void)
  244.     {
  245.     return m_fPrintSetup;
  246.     }
  247.  
  248.  
  249.  
  250.  
  251.  
  252. /*
  253.  * CPatronDoc::FQueryObjectSelected
  254.  *
  255.  * Purpose:
  256.  *  Returns whether or not there is an object selected in this
  257.  *  document for Cut, Copy, Delete functions.
  258.  *
  259.  * Parameters:
  260.  *  hMenu           HMENU of the Edit menu.
  261.  *
  262.  * Return Value:
  263.  *  BOOL            TRUE if we have an object, FALSE otherwise.
  264.  */
  265.  
  266. BOOL CPatronDoc::FQueryObjectSelected(HMENU hMenu)
  267.     {
  268.     return m_pPG->FQueryObjectSelected(hMenu);
  269.     }
  270.  
  271.  
  272.  
  273.  
  274.  
  275. /*
  276.  * CPatronDoc::ULoad
  277.  *
  278.  * Purpose:
  279.  *  Loads a given document without any user interface overwriting the
  280.  *  previous contents of the editor.
  281.  *
  282.  * Parameters:
  283.  *  fChangeFile     BOOL indicating if we're to update the window title
  284.  *                  and the filename from using this file.
  285.  *  pszFile         LPSTR to the filename to load.  Could be NULL for
  286.  *                  an untitled document.
  287.  *
  288.  * Return Value:
  289.  *  UINT            An error value from DOCERR_*
  290.  */
  291.  
  292.  
  293. UINT CPatronDoc::ULoad(BOOL fChangeFile, LPSTR pszFile)
  294.     {
  295.     RECT        rc;
  296.     LPSTORAGE   pIStorage;
  297.     HRESULT     hr;
  298.     CLSID       clsID;
  299.     DWORD       dwMode=STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
  300.  
  301.     if (NULL==pszFile)
  302.         {
  303.         //Create a new temp file.
  304.         hr=StgCreateDocfile(NULL, dwMode | STGM_CREATE | STGM_DELETEONRELEASE
  305.             , 0, &pIStorage);
  306.  
  307.         //Mark this as one of our class since we check with ReadClassStg below.
  308.         if (SUCCEEDED(hr))
  309.             WriteClassStg(pIStorage, CLSID_PatronPages);
  310.         }
  311.     else
  312.         {
  313.         hr=StgOpenStorage(pszFile, NULL, dwMode, NULL, 0, &pIStorage);
  314.         }
  315.  
  316.     if (FAILED(hr))
  317.         return DOCERR_COULDNOTOPEN;
  318.  
  319.     //Check if this is our type of file and exit if not.
  320.     hr=ReadClassStg(pIStorage, &clsID);
  321.  
  322.     if (FAILED(hr) || !IsEqualCLSID(clsID, CLSID_PatronPages))
  323.         {
  324.         pIStorage->Release();
  325.         return DOCERR_READFAILURE;
  326.         }
  327.  
  328.     //Attempt to create our contained Pages window.
  329.     m_pPG=new CPages(m_hInst, m_cf);
  330.     GetClientRect(m_hWnd, &rc);
  331.  
  332.     if (!m_pPG->FInit(m_hWnd, &rc, WS_CHILD | WS_VISIBLE, ID_PAGES, NULL))
  333.         {
  334.         pIStorage->Release();
  335.         return DOCERR_READFAILURE;
  336.         }
  337.  
  338.     if (!m_pPG->FIStorageSet(pIStorage, FALSE, (BOOL)(NULL==pszFile)))
  339.         {
  340.         pIStorage->Release();
  341.         return DOCERR_READFAILURE;
  342.         }
  343.  
  344.     //Open the window up for drag-drop
  345.     m_pDropTarget=new CDropTarget(this);
  346.  
  347.     if (NULL!=m_pDropTarget)
  348.         {
  349.         m_pDropTarget->AddRef();
  350.         RegisterDragDrop(m_hWnd, (LPDROPTARGET)m_pDropTarget);
  351.         CoLockObjectExternal((LPUNKNOWN)m_pDropTarget, TRUE, FALSE);
  352.         }
  353.  
  354.     m_pIStorage=pIStorage;
  355.     Rename(pszFile);
  356.  
  357.     //Do initial setup if this is a new file, otherwise Pages handles things.
  358.     if (NULL==pszFile)
  359.         {
  360.         //Go initialize the Pages for the default printer.
  361.         if (!PrinterSetup(NULL, TRUE))
  362.             return DOCERR_COULDNOTOPEN;
  363.  
  364.         //Go create an initial page.
  365.         m_pPG->PageInsert(0);
  366.         }
  367.     else
  368.         m_fPrintSetup=FALSE;    //Can't change an already saved configuration
  369.  
  370.     FDirtySet(FALSE);
  371.     return DOCERR_NONE;
  372.     }
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380. /*
  381.  * CPatronDoc::USave
  382.  *
  383.  * Purpose:
  384.  *  Writes the file t